﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Domain.MainModule.Entities;
using Infraestructure.CrossCutting.IoC;
using Infraestructure.CrossCutting.Logging;
using DistributedServices.Core.ErrorHandlers;
using System.ServiceModel;
using DistributedServices.MainModule.DTO;
using Application.MainModule.CustomersMTSManagement;

namespace DistributedServices.MainModule
{
    public partial class MainModuleService
    {
        public CustomerMTS GetCustomerMTSByUserId(string firstName, string lastName, string firstSurname, string lastSurname)
        {
            try
            {
                ICustomerMTSManagementService customerService = IoCFactory.Instance.CurrentContainer.Resolve<ICustomerMTSManagementService>();

                return customerService.FindCustomerMTSByName(firstName, lastName, firstSurname, lastSurname);
            }
            catch (ArgumentNullException ex)
            {
                ITraceManager traceManager = IoCFactory.Instance.CurrentContainer.Resolve<ITraceManager>();
                traceManager.TraceError(ex.Message);

                ServiceError detailedError = new ServiceError()
                {
                    ErrorMessage = Resources.Messages.exception_InvalidArguments
                };

                throw new FaultException<ServiceError>(detailedError);
            }
        }

        public void ChangeCustomerMTS(CustomerMTS customer)
        {
            try
            {
                ICustomerMTSManagementService customerService = IoCFactory.Instance.CurrentContainer.Resolve<ICustomerMTSManagementService>();
                customerService.ChangeCustomerMTS(customer);
            }
            catch (ArgumentNullException ex)
            {
                ITraceManager traceManager = IoCFactory.Instance.CurrentContainer.Resolve<ITraceManager>();
                traceManager.TraceError(ex.Message);

                ServiceError detailedError = new ServiceError()
                {
                    ErrorMessage = Resources.Messages.exception_InvalidArguments
                };

                throw new FaultException<ServiceError>(detailedError);
            }

        }

        public void RemoveCustomerMTS(CustomerMTS customer)
        {
            try
            {
                ICustomerMTSManagementService customerService = IoCFactory.Instance.CurrentContainer.Resolve<ICustomerMTSManagementService>();
                customerService.RemoveCustomerMTS(customer);
            }
            catch (ArgumentNullException ex)
            {
                ITraceManager traceManager = IoCFactory.Instance.CurrentContainer.Resolve<ITraceManager>();
                traceManager.TraceError(ex.Message);

                ServiceError detailedError = new ServiceError()
                {
                    ErrorMessage = Resources.Messages.exception_InvalidArguments
                };

                throw new FaultException<ServiceError>(detailedError);
            }
        }

        public void AddCustomerMTS(CustomerMTS customer)
        {
            try
            {
                ICustomerMTSManagementService customerService = IoCFactory.Instance.CurrentContainer.Resolve<ICustomerMTSManagementService>();
                customerService.AddCustomerMTS(customer);
            }
            catch (ArgumentNullException ex)
            {
                ITraceManager traceManager = IoCFactory.Instance.CurrentContainer.Resolve<ITraceManager>();
                traceManager.TraceError(ex.Message);

                ServiceError detailedError = new ServiceError()
                {
                    ErrorMessage = Resources.Messages.exception_InvalidArguments
                };

                throw new FaultException<ServiceError>(detailedError);
            }
        }

        public List<CustomerMTS> GetPagedCustomerMTS(PagedCriteria pagedCriteria)
        {
            try
            {
                ICustomerMTSManagementService customerService = IoCFactory.Instance.CurrentContainer.Resolve<ICustomerMTSManagementService>();

                return customerService.FindPagedCustomerMTS(pagedCriteria.PageIndex, pagedCriteria.PageCount);
            }
            catch (ArgumentException ex)
            {
                ITraceManager traceManager = IoCFactory.Instance.CurrentContainer.Resolve<ITraceManager>();
                traceManager.TraceError(ex.Message);

                ServiceError detailedError = new ServiceError()
                {
                    ErrorMessage = Resources.Messages.exception_InvalidArguments
                };

                throw new FaultException<ServiceError>(detailedError);
            }
            catch (NullReferenceException ex)
            {
                ITraceManager traceManager = IoCFactory.Instance.CurrentContainer.Resolve<ITraceManager>();
                traceManager.TraceError(ex.Message);

                ServiceError detailedError = new ServiceError()
                {
                    ErrorMessage = Resources.Messages.exception_InvalidArguments
                };

                throw new FaultException<ServiceError>(detailedError);
            }
        }

    }
}
